package com.moon.interceptor;

import com.moon.dao.AdminMapper;
import com.moon.entity.Admin;
import com.moon.utils.BaseController;
import com.moon.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@Component
public class AntiReLoginInterceptor extends BaseController implements HandlerInterceptor {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = super.getCookieVal(request, "token");
        Admin info = JwtUtils.getObject(token, Admin.class);
        Admin admin = adminMapper.selectByPrimaryKey(info.getId());
//        if(!admin.getLoginstr().equals(request.getSession().getId())){
//            request.getRequestDispatcher("/login").forward(request,response);
//            return false;
//        }
        //放行
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
